package com.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DButil {
    public static Connection getConn() {

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("数据库驱动加载成功");
            //jdbc:mysql://localhost:3306/test运行报错，
            // 要加上时区'?serverTimezone=UTC'
            String url = "jdbc:mysql://localhost:3306/zhaopin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
            Connection conn = DriverManager.getConnection(url, "root", "123456");
            return conn;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败" + e.getMessage());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败" + e.getMessage());
        }
        return null;
    }

    public static void closeDb( PreparedStatement p, Connection conn){
        if (p!=null){
            try {
                p.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

//    //	//提交事务
//    public static void commit() {
//        try {
//            conn.commit();
//        } catch (SQLException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }
}


